"use strict";
cc._RF.push(module, '42161NZmedCLpdiDH6/Q57y', 'Page');
// scripts/publicUI/Page.js

'use strict';

/**
 * 所有界面控制中心
 */

var PageEffect = {
    Normal: 0, //不带动作展开
    TransitionSlideInR: 1, //从右往左展开
    TransitionProgressInOut: 2 //从里面向外扩展变大展开
};

cc.Class({
    extends: cc.Component,

    properties: {
        pageEffectType: 0, //page的特效类型 默认0：直接直接展开 
        pageType: 1, //层级类型 默认1为普通界面 0为主界面
        isChatPage: false //是否聊天室
    },

    // use this for initialization
    onLoad: function onLoad() {
        if (this.pageType != 0) {
            this.node.on(cc.Node.EventType.TOUCH_START, function (event) {
                console.log('page TOUCH_START-' + this.node.name);
            }.bind(this), this);
            this.node.on(cc.Node.EventType.MOUSE_DOWN, function (event) {
                console.log('page MOUSE_DOWN-' + this.node.name);
            }.bind(this), this);
            this.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
                console.log('page TOUCH_MOVE-' + this.node.name);
            }.bind(this), this);
            this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
                console.log('page TOUCH_END-' + this.node.name);
            }.bind(this), this);
        }

        //相同界面唯一
        var canvas = cc.find("Canvas");
        var canvasChildren = canvas.children;
        var count = 0;
        for (var i = 0; i < canvasChildren.length; i++) {
            if (canvasChildren[i].name == this.node.name) {
                //  cc.log("[page] uid1:"+canvasChildren[i].uuid+" uid2:"+this.node.uuid );
                count++;
                if (count == 2) {
                    //   cc.log("【page】该界面已存在-remove:" + this.node.name);
                    this.node.destroy();
                    //this.node.removeFromParent();
                    return;
                }
            }
        }

        switch (this.pageEffectType) {
            case PageEffect.Normal:
                {
                    //  cc.log("直接显示");
                }
                break;
            case PageEffect.TransitionSlideInR:
                {
                    //    cc.log("右边展开");
                    var content = this.node.getChildByName("content");
                    if (content == null) return;
                    content.setPosition(1920, content.y);

                    this.node.runAction(cc.sequence(cc.delayTime(0.01), cc.callFunc(function () {
                        this.appear();
                    }.bind(this))));
                }
                break;
            case PageEffect.TransitionProgressInOut:
                {
                    //   cc.log("变大展开");
                    var content = this.node.getChildByName("content");
                    this.actionScaleIn = cc.sequence(cc.scaleTo(0.3, 1.05, 1.05).easing(cc.easeCubicActionOut()), cc.scaleTo(0.1, 1, 1).easing(cc.easeCubicActionOut()));
                    content.scale = 0.5;
                    content.runAction(this.actionScaleIn);
                }
                break;
        }

        var listener = {
            event: cc.EventListener.KEYBOARD,
            onKeyReleased: function (keyCode, event) {
                if (keyCode === cc.KEY.back) {
                    //返回键
                    var canvas = cc.find("Canvas");
                    var canvasChildren = canvas.children;
                    var highestZOrder = 0;
                    for (var i = 0; i < canvasChildren.length; i++) {
                        if (canvasChildren[i].getLocalZOrder() >= highestZOrder && canvasChildren[i].getLocalZOrder() < 40000) {
                            highestZOrder = canvasChildren[i].getLocalZOrder();
                        } else {
                            return;
                        }
                    }
                    //  cc.log("highestZOrder:"+highestZOrder+",getLocalZOrder:"+this.node.getLocalZOrder());
                    if (highestZOrder == this.node.getLocalZOrder()) {
                        if (highestZOrder == 0) {
                            this.onGameOver();
                        } else {
                            //     cc.log("page node.name:" + this.node.name);
                            if (this.node.getComponent(this.node.name) != null) {
                                if (this.isChatPage) {
                                    var betPanel = this.node.getChildByName("content").getChildByName("BetContent").getChildByName("BetPanelContent");
                                    var content = betPanel.getChildByName("BetScrollView").getChildByName("view").getChildByName("content");

                                    if (betPanel.active == true && content != null && content.children.length > 0) //存在投注面板
                                        {
                                            if (content.children[0].getComponent(content.children[0].name) != null) content.children[0].getComponent(content.children[0].name).onClose();
                                        } else //不存在投注面板
                                        {
                                            this.node.getComponent(this.node.name).onClose();
                                        }
                                } else {
                                    this.node.getComponent(this.node.name).onClose();
                                }
                            } else {
                                this.backAndRemove();
                            }
                        }
                    }
                }
            }.bind(this)
        };

        // 绑定键盘事件
        cc.eventManager.addListener(listener, this.node);

        this.node.runAction(cc.sequence(cc.delayTime(0.01), cc.callFunc(function () {
            if (this.pageType == 0) {
                this.node.setLocalZOrder(0);
                //this._tag = "panle"
                //    cc.log("page当前界面层级：" + this.node.name+ "->"+this.node.getLocalZOrder());   
            } else if (this.pageType == 1) {
                //this._tag = "page"
                //this.node.setLocalZOrder(ComponentsUtils.getPanelTag()); 
                //  cc.log("page当前界面层级：" + this.node.name+ "->"+this.node.getLocalZOrder());   
            }
        }.bind(this))));
    },

    appear: function appear() {
        var content = this.node.getChildByName("content");
        if (content == null) return;
        //content.setPosition(1080,content.y);
        content.runAction(cc.sequence(cc.moveTo(0.15, cc.p(0, content.y)), cc.callFunc(function () {
            this.isAppear = true;
        }.bind(this))));
    },

    backAndRemove: function backAndRemove() {
        var node = this.node;
        switch (this.pageEffectType) {
            case PageEffect.Normal:
                {
                    this.node.destroy();
                }
                break;
            case PageEffect.TransitionSlideInR:
                {
                    var screenSize = cc.view.getVisibleSize();
                    //var content = this.node.getChildByName("content"); 
                    this.node.runAction(cc.sequence(cc.moveTo(0.15, cc.p(screenSize.width, this.node.y)), cc.callFunc(function () {
                        cc.log("page node.name:" + node.name);
                        //this.node.children.length =0;
                        this.node.destroy();
                    }.bind(this), this)));
                }
                break;
            case PageEffect.TransitionProgressInOut:
                {
                    this.node.destroy();
                }
                break;
        }
    },

    onGameOver: function onGameOver() {
        var bEnd = false;
        var confirmCallback = function () {

            if (bEnd) {
                return;
            }
            bEnd = true;
            cc.director.end();
            //cc.game.end();
        }.bind(this);
        var closeCallback = function () {}.bind(this);
        ComponentsUtils.showAlertTips(2, "确定是否退出彩乐", closeCallback, "是否退出彩乐", confirmCallback);
    }

});

cc._RF.pop();